/*     */ package com.zimbra.cs.iochannel;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.TaskUtil;
/*     */ import com.zimbra.cs.session.PendingModifications;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.session.SessionCache;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.util.Collection;
/*     */ import java.util.concurrent.ExecutorService;
/*     */ import java.util.concurrent.Executors;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MailboxNotification
/*     */   extends Message
/*     */ {
/*     */   public static final String AppId = "mbn";
/*  36 */   private static final ExecutorService executor = Executors.newCachedThreadPool(TaskUtil.newDaemonThreadFactory("MailboxNotification"));
/*     */   private String accountId;
/*     */   
/*  39 */   public static MailboxNotification create(String accountId, int changeId, byte[] data) throws MessageChannelException { return new MailboxNotification(accountId, changeId, data); }
/*     */   
/*     */   private int changeId;
/*     */   private byte[] payload;
/*     */   protected int size()
/*     */   {
/*  45 */     return this.accountId.length() + 4 + this.payload.length + 8;
/*     */   }
/*     */   
/*     */   protected void serialize(ByteBuffer buffer)
/*     */     throws IOException
/*     */   {
/*  51 */     writeString(buffer, this.accountId);
/*  52 */     buffer.putInt(this.changeId);
/*  53 */     writeBytes(buffer, this.payload);
/*     */   }
/*     */   
/*     */ 
/*     */   protected Message construct(ByteBuffer buffer)
/*     */     throws IOException
/*     */   {
/*  60 */     return new MailboxNotification(buffer);
/*     */   }
/*     */   
/*     */   public String getAppId()
/*     */   {
/*  65 */     return "mbn";
/*     */   }
/*     */   
/*     */   public String getRecipientAccountId()
/*     */   {
/*  70 */     return this.accountId;
/*     */   }
/*     */   
/*     */   public byte[] getPayload() {
/*  74 */     return this.payload;
/*     */   }
/*     */   
/*     */   public int getChangeId() {
/*  78 */     return this.changeId;
/*     */   }
/*     */   
/*     */   MailboxNotification() {}
/*     */   
/*     */   public MailboxNotification(ByteBuffer buffer)
/*     */     throws IOException
/*     */   {
/*  86 */     this.accountId = readString(buffer);
/*  87 */     this.changeId = buffer.getInt();
/*  88 */     this.payload = readBytes(buffer);
/*     */   }
/*     */   
/*     */   protected void writeBytes(ByteBuffer buffer, byte[] data)
/*     */     throws IOException
/*     */   {
/*  94 */     buffer.putInt(data.length);
/*  95 */     buffer.put(data);
/*     */   }
/*     */   
/*     */   protected byte[] readBytes(ByteBuffer buffer) throws IOException {
/*  99 */     int len = buffer.getInt();
/* 100 */     ByteBuffer sub = buffer.slice();
/* 101 */     sub.limit(len);
/* 102 */     buffer.position(buffer.position() + len);
/* 103 */     byte[] data = new byte[len];
/* 104 */     sub.get(data);
/* 105 */     return data;
/*     */   }
/*     */   
/*     */   private MailboxNotification(String aid, int cid, byte[] ntfn)
/*     */   {
/* 110 */     this.accountId = aid;
/* 111 */     this.changeId = cid;
/* 112 */     this.payload = ntfn;
/*     */   }
/*     */   
/*     */   private void handleMailboxNotification(final MailboxNotification message) {
/* 116 */     executor.submit(new Runnable()
/*     */     {
/*     */       public void run() {
/* 119 */         Collection<Session> sessions = SessionCache.getAllSessions(message.getRecipientAccountId());
/* 120 */         if ((sessions == null) || (sessions.isEmpty())) {
/* 121 */           Message.log.warn("no active sessions for account %s", new Object[] { message.getRecipientAccountId() });
/* 122 */           return;
/*     */         }
/*     */         
/* 125 */         PendingModifications pms = null;
/* 126 */         for (Session session : sessions) {
/* 127 */           Message.log.debug("notifying session %s", new Object[] { session.toString() });
/* 128 */           if (pms == null) {
/*     */             try {
/* 130 */               pms = PendingModifications.deserialize(session.getMailbox(), message.getPayload());
/*     */             } catch (IOException e) {
/* 132 */               Message.log.warn("could not deserialize notification", e);
/* 133 */               return;
/*     */             } catch (ClassNotFoundException e) {
/* 135 */               Message.log.warn("could not deserialize notification", e);
/* 136 */               return;
/*     */             } catch (ServiceException e) {
/* 138 */               Message.log.warn("could not deserialize notification", e);
/* 139 */               return;
/*     */             }
/*     */           }
/* 142 */           session.notifyPendingChanges(pms, message.getChangeId(), null);
/*     */         }
/*     */       }
/*     */     });
/*     */   }
/*     */   
/*     */   public Message.MessageHandler getHandler()
/*     */   {
/* 150 */     new Message.MessageHandler()
/*     */     {
/*     */       public void handle(Message m, String clientId) {
/* 153 */         if (!(m instanceof MailboxNotification)) {
/* 154 */           return;
/*     */         }
/* 156 */         MailboxNotification.this.handleMailboxNotification((MailboxNotification)m);
/*     */       }
/*     */     };
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 163 */     StringBuilder buf = new StringBuilder();
/* 164 */     buf.append("mbn").append(":");
/* 165 */     buf.append(this.accountId).append(":");
/* 166 */     buf.append(this.changeId).append(":");
/* 167 */     buf.append(this.payload);
/* 168 */     return buf.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/iochannel/MailboxNotification.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */